﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using DTO;
using DataService;

public partial class Admin_AddDevice :BasePage
{
    private DeviceService service = new DeviceService();
    private IList<Device.OperatingSystem> OSs;
    private IList<Device.Memory> Memorys;
    private IList<Device.CPUs> CPUs;
    private IList<Device.DiskSpace> DiskPlaces;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (AdminLogin == null)
            Response.Redirect("~/admin/login.aspx");

        if (string.IsNullOrEmpty(Request.QueryString["RequestDeviceId"]))
            Response.Redirect("RequestDevice.aspx");

        OSs = service.GetListOS();
        Memorys = service.GetListMemory();
        CPUs = service.GetListCPU();
        DiskPlaces = service.GetListDiskPlace();
        if (!IsPostBack)
        {
            Initial();
            SetValueControl();
        }
    }
    private void Initial()
    {
        if (OSs != null)
        {
            ddlOS.DataSource = OSs;
        }
        if (Memorys != null)
        {
            ddlMemory.DataSource = Memorys;
        }
        if (CPUs != null)
        {
            ddlCpu.DataSource = CPUs;
        }
        if (DiskPlaces != null)
        {
            ddlDiskPlace.DataSource = DiskPlaces;
        }
        DataBind();
    }

    private void SetValueControl()
    {
        int RequestDeviceId = Convert.ToInt32(Request.QueryString["RequestDeviceId"]);
        Device device = service.GetListRequestByRequestId(RequestDeviceId);

        lbUser.Text = Convert.ToString(Request.QueryString["user"]); //device.user.Username;
        lbCustomer.Text = Convert.ToString(Request.QueryString["acc"]); //device.user.Customer.Code;

        lbPlanName.Text = "(Customize)";// device.Plan.Name;
        ddlCpu.Text = device.Plan.CPUs;
        ddlMemory.Text = device.Plan.RAM;
        ddlDiskPlace.Text = device.Plan.DiskQuota;
        ddlOS.SelectedValue = device.OS.Id.ToString();

    }
    protected void btUpdate_Click(object sender, EventArgs e)
    {
        int RequestDeviceId = Convert.ToInt32(Request.QueryString["RequestDeviceId"]);
        service.UpdateRequestDevice(RequestDeviceId, Convert.ToInt32(ddlCpu.SelectedValue), Convert.ToInt32(ddlMemory.SelectedValue), Convert.ToInt32(ddlDiskPlace.SelectedValue),Convert.ToInt32(ddlOS.SelectedValue),txtBandWidth.Text.Trim());
        lblInfo.Text="Update Complete. Error redict ..?";
    }
    protected void btCancel_Click(object sender, EventArgs e)
    {
        int RequestDeviceId = Convert.ToInt32(Request.QueryString["RequestDeviceId"]);
        service.UpdateRequestDeviceStatus(RequestDeviceId,3);
    }
}